package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.C2BPreAuthRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BPreAuthResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 被扫预授权
 */
public class C2BPreAuthService extends PayService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BPreAuthRequest c2BPreAuthRequest = new C2BPreAuthRequest();
        ConvertUtil.convertOutput(c2BPreAuthRequest);
        return c2BPreAuthRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        C2BPreAuthResponse c2BPreAuthResponse = new C2BPreAuthResponse();
        C2BPreAuthRequest c2BPreAuthRequest = (C2BPreAuthRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(c2BPreAuthRequest);

        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
            c2BPreAuthResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            c2BPreAuthResponse.setVoucherNum(RandomUtil.randomNumStr(32));
            c2BPreAuthResponse.setAuthCode(RandomUtil.getRandomInt());
            PayerInfo payerInfo = new PayerInfo();
            payerInfo.setAccNo("6210960000000000021");
            payerInfo.setAcctClass("00");
            c2BPreAuthResponse.setPayerInfo(payerInfo);
            chnlBizResponse.setResult(c2BPreAuthResponse);
            return chnlBizResponse;
        }

        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, c2BPreAuthRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, c2BPreAuthRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            c2BPreAuthResponse.setRespCode("04");
            c2BPreAuthResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(c2BPreAuthResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(c2BPreAuthResponse.getRespCode())) {
            c2BPreAuthResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            chnlBizResponse.setResult(c2BPreAuthResponse);
            return chnlBizResponse;
        }
        String payInfoEnc = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr);
        if (payInfoEnc != null) {
            String payInfo = descryptData(payInfoEnc);
            rglog.error("解密的payInfo:<{}>", payInfo);
            c2BPreAuthResponse.setPayerInfo((PayerInfo) convertKvStr2Bean(payInfo.substring(1, payInfo.length() - 1), PayerInfo.class));
        }

        chnlBizResponse.setResult(c2BPreAuthResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BPreAuthResponse c2BPreAuthResponse = (C2BPreAuthResponse) response.getResult();
        ConvertUtil.convertInput(c2BPreAuthResponse);
    }

}
